#!/bin/sh

if ! command -v nmcli > /dev/null 2>&1; then
    echo "nmcli not found"
    exit 1
fi

if [ -z "$1" ] || [ "$1" = "help" ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
    echo "usage: wifi [list|ssid [wpa secret]]"
    exit 1
fi

if [ "$1" = "list" ]; then
    nmcli dev wifi list
    exit 0
fi

# May be the magic ticket
nmcli con add ifname wlan0 type wifi con-name "$1" ssid "$1"

# nmcli con add dev wifi type wifi con-name "$1" ssid "$1"

if [ ! -z "$2" ]; then
    nmcli con modify "$1" wifi-sec.key-mgmt wpa-psk
    nmcli con modify "$1" wifi-sec.psk "$2"
fi

nmcli con up "$1"
